//
//  TJFontStyle.m
//  BaoDian
//
//  Created by mac on 2024/8/23.
//

#import "TJFontStyle.h"

@interface TJFontStyle()

@property (nonatomic, strong) NSDictionary *fontDict;
@property (nonatomic, strong) UIFont *font;

@end

@implementation TJFontStyle

- (nullable NSMutableAttributedString *)setTextStyleWithText:(NSString *)text textColor:(NSString *)textColor fontName:(NSString *)fontName fontSize:(CGFloat)fontSize strokeColor:(NSString *)strokeColor strokeWidth:(NSInteger)strokeWidth shadowColor:(NSString *)shadowColor shadowBlurRadius:(NSInteger)shadowBlurRadius isItalic:(BOOL)italic isBold:(BOOL)bold borderStyle:(int)borderStyle underline:(BOOL)underline {
    
    if (!text) return nil;
    if (fontSize <= 0) {
        fontSize = 20;
    }
//    18: 32 =
//    strokeWidth = 1;
//    shadowBlurRadius = 1;
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
    
    NSString *_fontName = [self.fontDict objectForKey:fontName];
    UIFont *font = nil;
    if ([self isExistFontName:_fontName]) {
        
        NSArray *arr = [_fontName componentsSeparatedByString:@"-"];
        if (arr.count == 2) {//可以设置加粗
            if (bold) {
                _fontName = [NSString stringWithFormat:@"%@-Bold", arr.firstObject];
            }
        }
        font = [UIFont fontWithName:_fontName size:fontSize];
    } else {
        font = [UIFont systemFontOfSize:fontSize];
    }
    
    self.font = font;
    
    NSShadow *shadow = [[NSShadow alloc] init];
//    shadow.shadowColor = UIColorMakeWithHexString(shadowColor);  // 阴影颜色
    shadow.shadowOffset = CGSizeMake(2, 2); // 阴影偏移量
//    shadow.shadowBlurRadius = shadowBlurRadius;             // 阴影模糊半径
    

    if (borderStyle == 1) {
        [attributedString addAttributes:@{
            NSStrokeColorAttributeName: UIColorMakeWithHexString(strokeColor),
            NSStrokeWidthAttributeName: @(-strokeWidth),
            NSShadowAttributeName: shadow
        } range:NSMakeRange(0, attributedString.length)];
    }
    if (underline) {
        [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, attributedString.length)]; // 下划线
    }
    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, attributedString.length)]; // 文字颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorMakeWithHexString(textColor) range:NSMakeRange(0, attributedString.length)];
    return attributedString;
}


- (BOOL)isExistFontName:(NSString *)fontName {
    NSArray *fontFamilies = [UIFont familyNames];
    for (NSString *family in fontFamilies) {
        NSArray *fontNames = [UIFont fontNamesForFamilyName:family];
        for (NSString *fontNameValue in fontNames) {
            if ([fontNameValue isEqualToString:fontName]) {
                return YES;
            }
        }
    }
    return NO;
}

- (void)logAllFontName {
    NSArray *fontFamilies = [UIFont familyNames];
    for (NSString *family in fontFamilies) {
        NSLog(@"Family: %@", family);
        NSArray *fontNames = [UIFont fontNamesForFamilyName:family];
        for (NSString *fontName in fontNames) {
            NSLog(@"Font Name: %@", fontName);
        }
    }
}

- (NSDictionary *)fontDict {
    if (!_fontDict) {
        _fontDict = @{
            @"SimSun": @"SimSun",
            @"WenQuanYi MicroHei": @"WenQuanYiMicroHei",
            @"WenQuanYi Zen Hei Mono": @"WenQuanYiZenHeiMono",
            @"WenQuanYi Zen Hei Sharp": @"WenQuanYiZenHeiSharp",
            @"AlibabaPuHuiTi": @"AlibabaPuHuiTi-Regular",
            @"SiYuan Heiti": @"SourceHanSansCN-Regular",
            @"SiYuan SimSun": @"SourceHanSerifCN-Regular"
        };
    }
    return _fontDict;
}

@end
